﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;

namespace VMWareLabWorkflowsDataContracts
{
    using Microsoft.TeamFoundation.Build.Client;

    [BuildActivity(HostEnvironmentOption.Agent)]
    public sealed class SetAgentInformationNode : CodeActivity
    {
        [RequiredArgument]
        public InArgument<string> ExceptionMessage { get; set; }

        [RequiredArgument]
        public InArgument<bool> MSTestFailed { get; set; }

        protected override void Execute(CodeActivityContext context)
        {
            var buildDetail = context.GetExtension<IBuildDetail>();
            IBuildInformationNode informationNodes = buildDetail.Information.GetNodesByType(AgentInformationNode.Name).FirstOrDefault();
            AgentInformationNode agentInformationNode;
            if (informationNodes == null)
            {
                var node = buildDetail.Information.CreateNode();
                node.Type = AgentInformationNode.Name;
                agentInformationNode = new AgentInformationNode(node);
            }
            else
            {
                agentInformationNode = new AgentInformationNode(buildDetail.Information.GetNodesByType(AgentInformationNode.Name).FirstOrDefault());
            }

            agentInformationNode.MSTestFailed = context.GetValue(this.MSTestFailed);
            agentInformationNode.MSTestExceptionMessage = context.GetValue(this.ExceptionMessage);
            agentInformationNode.Save();
        }
    }
}
